$().ready(function() {
	validateRule();

	if( !$("#topicImage").val() ){
		$("#brandlogo1").html('');
	}

	if( !$("#topicVideo").val() ){
		$("#videoShow").html('');
	}

	if( !$("#columnImage").val() ){
		$("#columnImage1").html('');
	}

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/topicReport/updateTopicReport",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}

//上传品牌logo
layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnlogoAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {

			let str = `
                <div class="closeBtn">
                    <img class="upImg" src="${r.data}" width="80" height="80"/>
                    <span class="iconclose1"><img src="../../../img/close.png"></span>
                </div>
              `
			$("#brandlogo1").html(str)
			$("#topicImage").val(r.data);

			$(".iconclose1").click(function (){
				$("#brandlogo1").html('')
				$("#topicImage").val('')
			})
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#topicAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {

			let str = `
                <div class="closeBtn">
                    <img class="upImg" src="${r.data}" width="80" height="80"/>
                    <span class="iconclose2"><img src="../../../img/close.png"></span>
                </div>
              `
			$("#columnImage1").html(str)
			$("#columnImage").val(r.data);

			$(".iconclose2").click(function (){
				$("#columnImage1").html('')
				$("#columnImage").val('')
			})
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

layui.use(['upload', 'element', 'layer'], function() {
	var $ = layui.jquery
		, upload = layui.upload;

	upload.render({
		elem: '#video'
		, url: '/common/sysFile/upload'
		, accept: 'video' //视频
		, done: function (res) {
			$("#topicVideo").val(res.data);
			$("#videoShow").html(`
				<video width="200" height="200" src="${res.data}" controls autoplay>抱歉，您的浏览器不支持内嵌视频</video>
				<span class="iconclose" data-close="2"><img src="../../../img/close.png"></span>
			`);
			deleImg()
		}
	});

})

deleImg()
function deleImg(){
	$(".iconclose").click(function (){
		if( $(this).attr('data-close') == '1' ){
			$(this).parent('.closeBtn').remove();
			$("#brandlogo1").html('')
			$("#topicImage").val('')
		}else if( $(this).attr('data-close') == '3' ){
			$(this).parent('.closeBtn').remove();
			$("#columnImage1").html('')
			$("#columnImage").val('')
		}
		else if( $(this).attr('data-close') == '2' ){
			$(this).parent('.closeBtn').remove();
			$("#videoShow").html('')
			$("#topicVideo").val('')
		}

	})
}


$("#topicVideo").blur(function (){
	if( $(this).val() ){
		$("#videoShow").html(`
					<video width="200" height="200" src="${$(this).val()}" controls autoplay>抱歉，您的浏览器不支持内嵌视频</video>
					<span class="iconclose"><img src="../../../img/close.png"></span>
				`);

		$(".iconclose").click(function (){
			$(this).parent('.closeBtn').remove();
			$("#videoShow").html('')
			$("#topicVideo").val('')
		})
	}
})